<?
/*
This script is meant to replace the Linux ftp program
in the svn2ftp.sh script so that it reports correct
error codes.  The Linux program reports success even
upon failure.  It uploads and deletes files without
issue, but isn't so good at deleting folders.

 - Ameir Abdeldayem
 http://www.ameir.net
 You are free to use this script as you please so long
 as you keep my name and URL in it.

*/

$ftp_server=$argv[1];
$ftp_port=$argv[2];
$ftp_user_name=$argv[3];
$ftp_user_pass=$argv[4];
$ftp_initial_dir=$argv[5];

$directories=explode("\n",$argv[6]);
$files=explode("\n",$argv[7]);
$delete=explode("\n",$argv[8]);

// echo "$argv[1]\n";
$up = (empty($argv[7])) ? '0' : sizeof($files);
$del = (empty($argv[8])) ? '0' : sizeof($delete);
echo $up . " file(s) to upload, " . $del . " file(s) to delete\n";

//foreach ($files as $file)
//  echo $file;

// print_r($files);
// print_r($delete);


// set up basic connection
if (empty($ftp_port))
$ftp_port="21";
$conn_id = ftp_connect($ftp_server, $ftp_port);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
	echo "FTP connection has failed!\n";
	echo "Attempted to connect to $ftp_server for user $ftp_user_name\n";
	exit(1);
} else {
	echo "Connected to $ftp_server, for user $ftp_user_name\n";
}

// turn passive mode on
ftp_pasv($conn_id, true);

// try to change the directory to somedir
if (ftp_chdir($conn_id, $ftp_initial_dir)) {
	echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
	echo "Couldn't change directory\n";
}

if (!($directories[0] == NULL &&  sizeof($directories) == 1)) {
	foreach ($directories as $dir) {
		$mkdir = ftp_mkdir($conn_id, $dir);
		if ($mkdir) {
			echo "Created directory $dir\n";
		}
	}
}
// upload the file
if (!($files[0] == NULL &&  sizeof($files) == 1)) {
	$uploaderror=0;
	$filecount=1;
	foreach ($files as $file) {
		$destination_file = $source_file = $file;
		// $mkdir = ftp_mkdir($conn_id, dirname($destination_file));

		$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

		// check upload status
		if (!$upload) {
			echo "FTP upload of $destination_file has failed!\n";
			$uploaderror++;
			if ($uploaderror >=5) {
				echo "More than five upload errors have occured.  Exiting.\n";
				exit(1);
			}
		} else {
			echo "Uploaded $source_file to $ftp_server ($filecount of $up)\n";
			$filecount++;
		}
	}
}

if (!($delete[0] == NULL &&  sizeof($delete) == 1)) {
	$delete = array_reverse($delete); //flip array around so that files are deleted, then folders removed
	foreach ($delete as $file) {
		$remove = ftp_delete($conn_id,$file);
		if (!$remove) {
			$rmdir = ftp_rmdir($conn_id,$file);
			if ($rmdir) {
				echo "Deleted $file from $ftp_server\n";
			}
		} else {
			echo "Deleted $file from $ftp_server\n";
		}
	}
}
// close the FTP stream
ftp_close($conn_id);

/*
 * cut from shell script:
 ftp -nv <<EOF
 open $FTPHOST
 user $FTPUSER $FTPPASS
 binary
 cd $FTPDIR
 $MKDIR
 $DELETE
 $ATTACH
 quit
 EOF

 # Send updates to second server if it is configured
 if [ -n "$FTPHOST1" ]; then
 ftp -nv <<EOF
 open $FTPHOST1
 user $FTPUSER1 $FTPPASS1
 binary
 cd $FTPDIR1
 $MKDIR
 $DELETE
 $ATTACH
 quit
 EOF
 fi

 */
?>

